<?php

use yii\helpers\Url;


/* @var $this yii\web\View */
/* @var $model app\models\Recall */
/* @var $form yii\widgets\ActiveForm */
?>
<style type="text/css">
    #select_datetime_button td .layui-btn-xs {
        width: 50px;
        height: 28px;
        line-height: 28px;
        display: inline-block;
        border: #999 1px solid;
        border-radius: 5px;
        padding: 0 5px;
        text-align: center;
        cursor: pointer;
    }
</style>

<div class="recall-form" id="review_id" style="display: none;">
    <form class="layui-form layui-form-pane" style="padding:1em;" id="review" style="width:500px;padding:20px;" lay-filter="create">
        <div class="layui-form-item">
            <input type="hidden" name="patient_id" value="" />
            <textarea name="content" placeholder="请输入内容" class="layui-textarea" required lay-verify="required"></textarea>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">回访类型</label>
            <div class="layui-input-block">
                <input type="radio" name="review_type" value="例行回访" title="例行回访" checked="checked" lay-filter="filter" />
                <input type="radio" name="review_type" value="待跟进" title="待跟进" lay-filter="filter" />
                <input type="radio" name="review_type" value="已改约" title="已改约" lay-filter="filter" />
                <input type="radio" name="review_type" value="流失" title="流失" lay-filter="filter" />
                <input type="radio" name="review_type" value="其他" title="其他" lay-filter="filter" />
            </div>
        </div>
        <div class="layui-form-item" id="review_order_time_div" style="display: none;">
            <label class="layui-form-label">改约时间</label>
            <div class="layui-input-block">
                <input id="review_order_time" type="text" name="order_time" autocomplete="off" class="layui-input" />
            </div>
            <table id="select_datetime_button" style="margin:0 0.5em;width:100%;">
                <tr>
                    <td style="padding:0.5em;border-bottom:#ddd 1px solid;">
                        <div onclick="select_date('<?= date('Y-m-d') ?>')" class="layui-btn-xs">今天</div>
                        <div onclick="select_date('<?= date('Y-m-d', strtotime('+1 day')) ?>')" class="layui-btn-xs">明天</div>
                        <div onclick="select_date('<?= date('Y-m-d', strtotime('+2 day')) ?>')" class="layui-btn-xs">后天</div>
                        <div onclick="select_date('<?= date('Y-m-d', strtotime('+3 day')) ?>')" class="layui-btn-xs">大后天</div>
                        <div onclick="select_date('<?= date('Y-m-d', strtotime('next Saturday')) ?>')" class="layui-btn-xs">周六</div>
                        <div onclick="select_date('<?= date('Y-m-d', strtotime('next Sunday')) ?>')" class="layui-btn-xs">周日</div>
                        <div onclick="select_date('<?= date('Y-m-d', strtotime('next Monday')) ?>')" class="layui-btn-xs">周一</div>
                        <div onclick="select_date('<?= date('Y-m-d', strtotime('+7 day')) ?>')" class="layui-btn-xs">一周后</div>
                        <div onclick="select_date('<?= date('Y-m-d', strtotime('+15 day')) ?>')" class="layui-btn-xs">半月后</div>
                    </td>
                </tr>
                <tr>
                    <td style="padding:0.5em;">
                        <div onclick="select_time('08:00:00')" class="layui-btn-xs">8点</div>
                        <div onclick="select_time('09:00:00')" class="layui-btn-xs">9点</div>
                        <div onclick="select_time('10:00:00')" class="layui-btn-xs">10点</div>
                        <div onclick="select_time('11:00:00')" class="layui-btn-xs">11点</div>
                        <div onclick="select_time('12:00:00')" class="layui-btn-xs">12点</div>
                        <div onclick="select_time('13:00:00')" class="layui-btn-xs">13点</div>
                        <div onclick="select_time('14:00:00')" class="layui-btn-xs">14点</div>
                        <div onclick="select_time('15:00:00')" class="layui-btn-xs">15点</div>
                        <div onclick="select_time('16:00:00')" class="layui-btn-xs">16点</div>
                        <div onclick="select_time('17:00:00')" class="layui-btn-xs">17点</div>
                        <div onclick="select_time('18:00:00')" class="layui-btn-xs">18点</div>
                    </td>
                </tr>
            </table>
        </div>
        <div class="layui-form-item button" style="position: absolute;bottom:5px;right:20px;">
            <button class="layui-btn" lay-submit lay-filter="formReviewBeforeSubmit" style="width: 100%;">
                保 存
            </button>
        </div>
    </form>
</div>

<script type="text/javascript">
    window.setTimeout("init_review_layer()", 500);

    function init_review_layer() {
        window.review_content_html = $('#review_id').html();
        $('#review_id').remove();
    }

    function patient_review(patient_name, patient_id) {
        layui.layer.open({
            type: 1,
            title: '回访患者: ' + patient_name,
            area: ['650px', '440px'],
            content: window.review_content_html,
        });
        layui.laydate.render({
            min: '2021-01-01',
            elem: '#review_order_time',
            type: 'datetime',
            isInitValue: true
        });
        layui.form.on('radio(filter)', function(data) {
            if (data.elem.checked && data.value == '已改约') {
                $('#review_order_time_div').show();
            } else {
                $('#review_order_time_div').hide();
            }
        });
        layui.form.on('submit(create)', function() {
            var data = layui.form.val('create');
            window.patient_reiview(data);
            return false;
        });
        layui.form.val("create", {
            "patient_id": patient_id
        });
        return layui.form.render();
    }

    window.patient_reiview = function(form_data) {
        form_data._csrf = '<?= Yii::$app->request->csrfToken ?>';
        $.ajax({
            url: "<?= Url::toRoute('recall/create') ?>",
            data: form_data,
            dataType: 'json',
            type: 'post',
            async: false,
            success: function(res) {
                layui.layer.msg(res.msg);
                if (res.status == 'success') {
                    if ($('button[lay-filter="formBeforeSubmit"]').length > 0) {
                        return $('button[lay-filter="formBeforeSubmit"]').click();
                    } else {
                        return window.location.reload();
                    }
                }

            }
        });
    };

    function select_date(value_date) {
        $('#review_order_time').val(value_date + ' 00:00:00');
    }

    function select_time(value_time) {
        var selected_date = $('#review_order_time').val();
        if (selected_date.length < 10) {
            layer.alert('先选择日期，才能选择时间', {
                icon: 2
            });
            return false;
        }
        $('#review_order_time').val(selected_date.slice(0, 10) + ' ' + value_time);
    }
</script>